package com.dangz.dans.controller;

import com.dangz.dans.entity.CommonResponse;
import com.dangz.dans.response.ApiEnum;
import com.dangz.dans.response.CodeEnum;
import com.dangz.dans.response.TypeEnum;
import com.dangz.dans.utils.Constants;
import com.dangz.dans.utils.file.DownFileFetch;
import com.dangz.dans.utils.file.DownFileInfoBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;

/**
 * @author dangz
 * @since 2018/3/8 10:20
 */
@RestController
@RequestMapping("dans/file")
@Api(tags = "多线程处理文件")
public class FileController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ApiOperation("本地上传文件")
    /*@ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", value = "Authorization", name = HttpHeaders.AUTHORIZATION, dataType = "String")
    })*/
    @PostMapping(value = "upload")
    public CommonResponse upload(@RequestParam(value = "file", required = true) MultipartFile f) {
        logger.info("filename:{}, name:{}", f.getOriginalFilename(), f.getName());
        CommonResponse commonResponse = null;
        InputStream ins = null;
        OutputStream os = null;
        try {
            //通过文件输入输出流 上传文件
            File file = new File(f.getOriginalFilename());

            ins = f.getInputStream();
            os = new FileOutputStream(file);

            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }

            //多线程分块处理文件  提升效率
            DownFileInfoBean bean = new DownFileInfoBean(null, Constants.UPLOAD_FILE_PATH,
                    f.getOriginalFilename(), 5, false, file);
            DownFileFetch fileFetch = new DownFileFetch(bean);
            fileFetch.start();

            os.close();
            ins.close();
            commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
                    ApiEnum.XTGL, CodeEnum.SUCCESS_SERVICE_LOCAL), CodeEnum.SUCCESS_SERVICE_LOCAL.getInfo(), bean);
        } catch (Exception e) {
            e.printStackTrace();
            commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Error,
                    ApiEnum.XTGL, CodeEnum.FAIL_SERVICE_LOCAL), CodeEnum.FAIL_SERVICE_LOCAL.getInfo());
        }
        return commonResponse;
    }

    @ApiOperation("从URL下载文件")
    @ApiImplicitParams({
            //@ApiImplicitParam(paramType = "header", value = "Authorization", name = HttpHeaders.AUTHORIZATION, dataType = "String")
            @ApiImplicitParam(paramType = "query", value = "URL路径", name = "filePath", dataType = "String", required = true)
//            @ApiImplicitParam(paramType = "query", value = "存放路径", name = "savePath", dataType = "String", required = true)
    })
    @PostMapping(value = "download")
    public CommonResponse download(String filePath) {
        CommonResponse commonResponse = null;
        String fileName = filePath.substring(filePath.lastIndexOf('/') + 1, filePath.length());
        DownFileFetch fileFetch = null;
        try {
            DownFileInfoBean bean = new DownFileInfoBean(filePath, Constants.DOWNLOAD_FILE_PATH, fileName, 5, true, null);
            fileFetch = new DownFileFetch(bean);
            fileFetch.start();
            commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
                    ApiEnum.XTGL, CodeEnum.SUCCESS_SERVICE_LOCAL), CodeEnum.SUCCESS_SERVICE_LOCAL.getInfo(), bean);
        } catch (IOException e) {
            e.printStackTrace();
            commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Error,
                    ApiEnum.XTGL, CodeEnum.FAIL_SERVICE_LOCAL), CodeEnum.FAIL_SERVICE_LOCAL.getInfo());
        }
        return commonResponse;
    }
}
